<?php
/**
 * Add a Customer on the flow in the light mode by passing the name only
 *
 */

// Retrieve Customer Type
use Fox\Common\Customer;
use Fox\Html\Screen;
$type = Xview::instance()->val("type");

$callback = Fox\Generator::ajaxReload("sh:Customer_Competitor",
									Fox\Generator::url("inquiry","competitors","add"));
$screen = new Screen("newCustomer","create",null,true,$callback,Screen::FADER_HIDE_AFTER_SUBMIT);
$block = Xview::instance()->component("customer","name");
$block->add(new Fox\Html\Input("type",true,$type))->hidden();
$block->item("name2")->hidden();
$block->item("function")->hidden();
$screen->add($block);
if(isset($_POST['name1']))
{
	$inputs = $screen->get();
	if(!$screen->errors())
	{
		// Create Customer
		try {
			$customer = Customer::findByNameAndType($inputs['name1'],Customer::TYPE_COMPETITOR);
			if($customer->exists())
			{
				throw new Exception(Msg(Fox\Msg::BUNDLE,Fox\Msg::MSG_DUPLICATE));
			}

			$object = new Customer();
			$object->setFields($inputs);
			$object->create();
			Fox\Message::Success(Msg(Fox\Msg::BUNDLE,Fox\Msg::MSG_CREATE,$object->name1));
			exit;
		}
		catch (Fox\Object\Exception\Save $e)
		{
			Fox\Message::Error($e->getMessage());
		}
		catch (Exception $e)
		{
			Fox\Message::Error($e->getMessage());
		}
	}
}
$title = Msg(Fox\Msg::BUNDLE,Fox\Msg::MSG_ADD_A_NEW_OBJECT_TITLE,
		Msg(Customer::BUNDLE_NAME,18));
$popup = new Fox\Popup("newCustomer",$title,$screen);
echo $popup->display();